"""
*******************************************************************************
                读取文件工具
*******************************************************************************
"""
import os


class DirTool:

    def __init__(self, main_dir):
        self.dir_list = []
        for filepath, dirnames, filenames in os.walk(fr'{main_dir}'):
            for filename in filenames:
                self.dir_list.append(os.path.join(filepath, filename))

    def get_dirs(self, include_filenames=None):
        """
        获取指定目录下的文件列表
        :param include_filenames: 指定文件类型
        :return:
        """
        dirs = []
        if include_filenames:
            for dir_name in self.dir_list:
                is_exist = False
                for in_name in include_filenames:
                    if in_name in dir_name:
                        is_exist = True
                        break
                if is_exist:
                    dirs.append(dir_name)
        else:
            dirs = self.dir_list
        return dirs

    def get_dir_and_exclude(self, exclude_filenames=None):
        """
        获取指定目录下的文件列表(含排除类型)
        :param exclude_filenames: 排除文件类型
        :return:
        """
        dirs = []
        if exclude_filenames:
            for dir_name in self.dir_list:
                not_exist = True
                for fn in exclude_filenames:
                    if fn in dir_name:
                        not_exist = False
                        break
                if not_exist:
                    dirs.append(dir_name)
        else:
            dirs = self.dir_list
        return dirs
